home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / archiver / zipds10.zip / ZIPDS.PAS < prev   
Pascal/Delphi Source File  |  1989-02-18  |  7KB  |  290 lines

  1.  
  2. (*
  3.  * (C) 1989 Samuel H. Smith, 15-feb-89 (rev. 18-feb-89)
  4.  *
  5.  * This program is provided courtesy of:
  6.  *         The Tool Shop
  7.  *         Phoenix, Az
  8.  *         (602) 279-2673
  9.  *
  10.  * Disclaimer
  11.  * ----------
  12.  *
  13.  * If you modify this program, I would appreciate a copy of the new 
  14.  * source code.   Please don't delete my name from the program.
  15.  *
  16.  * I cannot be responsible for any damages resulting from the use or mis-
  17.  * use of this program!
  18.  *
  19.  * If you have any questions, bugs, or suggestions, please contact me at
  20.  * The Tool Shop,  (602) 279-2673.
  21.  *
  22.  * Enjoy!     Samuel H. Smith
  23.  *
  24.  *)
  25.  
  26.  
  27. {$r-,s-}            (* enable range checking *)
  28. {$v-}               (* allow variable length string params *)
  29. {$D+,L+}
  30.  
  31. uses
  32.    Dos,MDosIO;
  33.  
  34. const
  35.    whoami   = 'ZIPDS: Zipfile Date Stamper  v1.0 02-18-89;  (C) 1989 S.H.Smith';
  36.  
  37.  
  38. (* libraries *)
  39.  
  40. {$i \tinc\anystring.inc}
  41. {$i \tinc\rempath.inc}  
  42.  
  43.  
  44. type
  45.    signature_type = longint;
  46.  
  47. const
  48.    local_file_header_signature = $04034b50;
  49.  
  50. type
  51.    local_file_header = record
  52.       version_needed_to_extract:    word;
  53.       general_purpose_bit_flag:     word;
  54.       compression_method:           word;
  55.       last_mod_file_time:           word;
  56.       last_mod_file_date:           word;
  57.       crc32:                        longint;
  58.       compressed_size:              longint;
  59.       uncompressed_size:            longint;
  60.       filename_length:              word;
  61.       extra_field_length:           word;
  62.    end;
  63.  
  64. const
  65.    central_file_header_signature = $02014b50;
  66.  
  67. type
  68.    central_directory_file_header = record
  69.       version_made_by:                 word;
  70.       version_needed_to_extract:       word;
  71.       general_purpose_bit_flag:        word;
  72.       compression_method:              word;
  73.       last_mod_file_time:              word;
  74.       last_mod_file_date:              word;
  75.       crc32:                           longint;
  76.       compressed_size:                 longint;
  77.       uncompressed_size:               longint;
  78.       filename_length:                 word;
  79.       extra_field_length:              word;
  80.       file_comment_length:             word;
  81.       disk_number_start:               word;
  82.       internal_file_attributes:        word;
  83.       external_file_attributes:        longint;
  84.       relative_offset_local_header:    longint;
  85.    end;
  86.  
  87. const
  88.    end_central_dir_signature = $06054b50;
  89.  
  90. type
  91.    end_central_dir_record = record
  92.       number_this_disk:                         word;
  93.       number_disk_with_start_central_directory: word;
  94.       total_entries_central_dir_on_this_disk:   word;
  95.       total_entries_central_dir:                word;
  96.       size_central_directory:                   longint;
  97.       offset_start_central_directory:           longint;
  98.       zipfile_comment_length:                   word;
  99.    end;
  100.  
  101. var
  102.    zipfd:      dos_handle;
  103.    zipfn:      dos_filename;
  104.    newdate:    word;
  105.    newtime:    word;
  106.    err:        integer;
  107.  
  108.  
  109. (* ---------------------------------------------------------- *)
  110. procedure get_string(len: word; var s: string);
  111. var
  112.    n: word;
  113. begin
  114.    if len > 255 then
  115.       len := 255;
  116.    n := dos_read(zipfd,s[1],len);
  117.    s[0] := chr(len);
  118. end;
  119.  
  120.  
  121. (* ---------------------------------------------------------- *)
  122. procedure process_local_file_header;
  123. var
  124.    n:             word;
  125.    rec:           local_file_header;
  126.    filename:      string;
  127.    extra:         string;
  128.  
  129. begin
  130.    n := dos_read(zipfd,rec,sizeof(rec));
  131.    get_string(rec.filename_length,filename);
  132.    get_string(rec.extra_field_length,extra);
  133.    dos_lseek(zipfd,rec.compressed_size,seek_cur);
  134.  
  135.    (* track newest member *)
  136.    if dos_jdate(rec.last_mod_file_time, rec.last_mod_file_date) >
  137.       dos_jdate(newtime,newdate) then
  138.    begin
  139.       newdate := rec.last_mod_file_date;
  140.       newtime := rec.last_mod_file_time;
  141.    end;
  142.  
  143. end;
  144.  
  145.  
  146. (* ---------------------------------------------------------- *)
  147. procedure process_central_file_header;
  148. var
  149.    n:             word;
  150.    rec:           central_directory_file_header;
  151.    filename:      string;
  152.    extra:         string;
  153.    comment:       string;
  154.  
  155. begin
  156.    n := dos_read(zipfd,rec,sizeof(rec));
  157.    get_string(rec.filename_length,filename);
  158.    get_string(rec.extra_field_length,extra);
  159.    get_string(rec.file_comment_length,comment);
  160. end;
  161.  
  162.  
  163. (* ---------------------------------------------------------- *)
  164. procedure process_end_central_dir;
  165. var
  166.    n:             word;
  167.    rec:           end_central_dir_record;
  168.    comment:       string;
  169.  
  170. begin
  171.    n := dos_read(zipfd,rec,sizeof(rec));
  172.    get_string(rec.zipfile_comment_length,comment);
  173. end;
  174.  
  175.  
  176. (* ---------------------------------------------------------- *)
  177. procedure process_headers;
  178. var
  179.    sig:  longint;
  180.    fail: integer;
  181.  
  182. begin
  183.    fail := 0;
  184.    newdate := 0;
  185.    newtime := 0;
  186.  
  187.    while true do
  188.    begin
  189.  
  190.       if dos_read(zipfd,sig,sizeof(sig)) <> sizeof(sig) then
  191.       begin
  192.          write(' Truncated!'^G);
  193.          inc(err);
  194.          exit;
  195.       end
  196.       else
  197.  
  198.       if sig = local_file_header_signature then
  199.          process_local_file_header
  200.       else
  201.  
  202.       if sig = central_file_header_signature then
  203.          process_central_file_header
  204.       else
  205.  
  206.       if sig = end_central_dir_signature then
  207.       begin
  208.          process_end_central_dir;
  209.          write(' Okay.');
  210.          exit;
  211.       end
  212.       else
  213.  
  214.       begin
  215.          write(' Bad header!'^G);
  216.          inc(err);
  217.          exit;
  218.       end;
  219.    end;
  220. end;
  221.  
  222.  
  223. (* ---------------------------------------------------------- *)
  224. procedure list_zip(dir,name: dos_filename);
  225. var
  226.    time,date:  word;
  227. begin
  228.    write(dir,name,':','':13-length(name));
  229.    zipfd := dos_open(dir+name,open_update);
  230.    if zipfd = dos_error then
  231.    begin
  232.       writeln(' Can''t open!');
  233.       exit;
  234.    end;
  235.  
  236.    process_headers;
  237.  
  238.    dos_file_times(zipfd,time_get,time,date);
  239.    if dos_jdate(time,date) <> dos_jdate(newtime,newdate) then
  240.    begin
  241.       write('  Stamping date.');
  242.       dos_file_times(zipfd,time_set,newtime,newdate);
  243.    end;
  244.  
  245.    dos_close(zipfd);
  246.    writeln;
  247. end;
  248.  
  249.  
  250. (* ---------------------------------------------------------- *)
  251. var
  252.    DirInfo:       SearchRec;
  253.    Dir,Nam,Ext:   dos_filename;
  254.  
  255. begin
  256.    writeln;
  257.    writeln(whoami);
  258.    writeln;
  259.  
  260.    if paramcount = 0 then
  261.    begin
  262.       writeln('Courtesy of:  S.H.Smith  and  The Tool Shop BBS,  (602) 279-2673.');
  263.       writeln;
  264.       writeln('Usage:  ZipDS *.zip [>OUT]');
  265.       writeln;
  266.       writeln('Sets non-0 errorlevel on truncated zipfiles.');
  267.       writeln('Stamps all zipfiles with date of newest member file.');
  268.       writeln;
  269.       halt(99);
  270.    end;
  271.  
  272.    err := 0;
  273.  
  274.    zipfn := paramstr(1);
  275.    if pos('.',zipfn) = 0 then
  276.       zipfn := zipfn + '.zip';
  277.  
  278.    FSplit(zipfn,Dir,Nam,Ext);
  279.    FindFirst(zipfn,$21,DirInfo);
  280.    while (DosError = 0) do
  281.    begin
  282.       list_zip(Dir,DirInfo.name);
  283.       FindNext(DirInfo);
  284.    end;
  285.  
  286.    writeln(err,' errors detected.');;
  287.    halt(err);
  288. end.
  289.  
  290.